/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Interaction;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Specimen;
import entityBeans.Interactiontype;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class InteractionJpaController {

    public InteractionJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Interaction interaction) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Specimen specimen = interaction.getSpecimen();
            if (specimen != null) {
                specimen = em.getReference(specimen.getClass(), specimen.getIdspecimen());
                interaction.setSpecimen(specimen);
            }
            Specimen specimen1 = interaction.getSpecimen1();
            if (specimen1 != null) {
                specimen1 = em.getReference(specimen1.getClass(), specimen1.getIdspecimen());
                interaction.setSpecimen1(specimen1);
            }
            Interactiontype interactiontype = interaction.getInteractiontype();
            if (interactiontype != null) {
                interactiontype = em.getReference(interactiontype.getClass(), interactiontype.getIdinteractiontype());
                interaction.setInteractiontype(interactiontype);
            }
            em.persist(interaction);
            if (specimen != null) {
                specimen.getInteractionList().add(interaction);
                specimen = em.merge(specimen);
            }
            if (specimen1 != null) {
                specimen1.getInteractionList().add(interaction);
                specimen1 = em.merge(specimen1);
            }
            if (interactiontype != null) {
                interactiontype.getInteractionList().add(interaction);
                interactiontype = em.merge(interactiontype);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Interaction interaction) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Interaction persistentInteraction = em.find(Interaction.class, interaction.getIdinteraction());
            Specimen specimenOld = persistentInteraction.getSpecimen();
            Specimen specimenNew = interaction.getSpecimen();
            Specimen specimen1Old = persistentInteraction.getSpecimen1();
            Specimen specimen1New = interaction.getSpecimen1();
            Interactiontype interactiontypeOld = persistentInteraction.getInteractiontype();
            Interactiontype interactiontypeNew = interaction.getInteractiontype();
            if (specimenNew != null) {
                specimenNew = em.getReference(specimenNew.getClass(), specimenNew.getIdspecimen());
                interaction.setSpecimen(specimenNew);
            }
            if (specimen1New != null) {
                specimen1New = em.getReference(specimen1New.getClass(), specimen1New.getIdspecimen());
                interaction.setSpecimen1(specimen1New);
            }
            if (interactiontypeNew != null) {
                interactiontypeNew = em.getReference(interactiontypeNew.getClass(), interactiontypeNew.getIdinteractiontype());
                interaction.setInteractiontype(interactiontypeNew);
            }
            interaction = em.merge(interaction);
            if (specimenOld != null && !specimenOld.equals(specimenNew)) {
                specimenOld.getInteractionList().remove(interaction);
                specimenOld = em.merge(specimenOld);
            }
            if (specimenNew != null && !specimenNew.equals(specimenOld)) {
                specimenNew.getInteractionList().add(interaction);
                specimenNew = em.merge(specimenNew);
            }
            if (specimen1Old != null && !specimen1Old.equals(specimen1New)) {
                specimen1Old.getInteractionList().remove(interaction);
                specimen1Old = em.merge(specimen1Old);
            }
            if (specimen1New != null && !specimen1New.equals(specimen1Old)) {
                specimen1New.getInteractionList().add(interaction);
                specimen1New = em.merge(specimen1New);
            }
            if (interactiontypeOld != null && !interactiontypeOld.equals(interactiontypeNew)) {
                interactiontypeOld.getInteractionList().remove(interaction);
                interactiontypeOld = em.merge(interactiontypeOld);
            }
            if (interactiontypeNew != null && !interactiontypeNew.equals(interactiontypeOld)) {
                interactiontypeNew.getInteractionList().add(interaction);
                interactiontypeNew = em.merge(interactiontypeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = interaction.getIdinteraction();
                if (findInteraction(id) == null) {
                    throw new NonexistentEntityException("The interaction with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Interaction interaction;
            try {
                interaction = em.getReference(Interaction.class, id);
                interaction.getIdinteraction();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The interaction with id " + id + " no longer exists.", enfe);
            }
            Specimen specimen = interaction.getSpecimen();
            if (specimen != null) {
                specimen.getInteractionList().remove(interaction);
                specimen = em.merge(specimen);
            }
            Specimen specimen1 = interaction.getSpecimen1();
            if (specimen1 != null) {
                specimen1.getInteractionList().remove(interaction);
                specimen1 = em.merge(specimen1);
            }
            Interactiontype interactiontype = interaction.getInteractiontype();
            if (interactiontype != null) {
                interactiontype.getInteractionList().remove(interaction);
                interactiontype = em.merge(interactiontype);
            }
            em.remove(interaction);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Interaction> findInteractionEntities() {
        return findInteractionEntities(true, -1, -1);
    }

    public List<Interaction> findInteractionEntities(int maxResults, int firstResult) {
        return findInteractionEntities(false, maxResults, firstResult);
    }

    private List<Interaction> findInteractionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Interaction.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }


    public Interaction findInteraction(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Interaction.class, id);
        } finally {
            em.close();
        }
    }

    public int getInteractionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Interaction> rt = cq.from(Interaction.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
